/*
 *    Copyright (c) 2018-2025, cloud All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: cloud
 */

package com.fuyao.cloud.asset.supplier.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fuyao.cloud.asset.dto.msupplier.AssetSupplierDTO;
import com.fuyao.cloud.asset.vo.AssetSupplierMExcelVO;
import com.fuyao.cloud.common.core.util.R;
import com.fuyao.cloud.common.data.mybatis.SpliceQueryWrapper;
import com.fuyao.cloud.common.log.annotation.SysLog;
import com.fuyao.cloud.asset.entity.AssetSupplier;
import com.fuyao.cloud.asset.supplier.service.AssetSupplierService;
import lombok.Builder;
import org.springframework.security.access.prepost.PreAuthorize;
import com.fuyao.cloud.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import com.fuyao.cloud.asset.vo.FyAssetSupplierExcelVO;
import java.util.List;
import java.util.UUID;

/**
 * 供应商管理
 *
 * @author 时间的朋友
 * @date 2021-11-17 14:47:38
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/fyassetsupplier" )
@Api(value = "fyassetsupplier", tags = "供应商管理管理")
public class AssetSupplierController {

    private final AssetSupplierService fyAssetSupplierService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param fyAssetSupplier 供应商管理
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('msupplier_fyassetsupplier_view','msupplier_fyassetsupplier_view')" )
    public R getFyAssetSupplierPage(Page page, AssetSupplierDTO fyAssetSupplier) {
        return R.ok(fyAssetSupplierService.page(page, SpliceQueryWrapper.spliceSearchCondition(fyAssetSupplier)));
    }


    /**
     * 通过id查询供应商管理
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('msupplier_fyassetsupplier_view','vsupplier_fyassetsupplier_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(fyAssetSupplierService.getById(id));
    }

    /**
     * 新增供应商管理
     * @param fyAssetSupplier 供应商管理
     * @return R
     */
    @ApiOperation(value = "新增供应商管理", notes = "新增供应商管理")
    @SysLog("新增供应商管理" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('msupplier_fyassetsupplier_add','vsupplier_fyassetsupplier_add')" )
    public R save(@RequestBody AssetSupplier fyAssetSupplier) {
        fyAssetSupplierService.save(fyAssetSupplier);
        return R.ok(fyAssetSupplier);
    }

    /**
     * 修改供应商管理
     * @param fyAssetSupplier 供应商管理
     * @return R
     */
    @ApiOperation(value = "修改供应商管理", notes = "修改供应商管理")
    @SysLog("修改供应商管理" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('msupplier_fyassetsupplier_edit','vsupplier_fyassetsupplier_edit')" )
    public R updateById(@RequestBody AssetSupplier fyAssetSupplier) {
        return R.ok(fyAssetSupplierService.updateById(fyAssetSupplier));
    }

    /**
     * 通过id删除供应商管理
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除供应商管理", notes = "通过id删除供应商管理")
    @SysLog("通过id删除供应商管理" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('msupplier_fyassetsupplier_del','vsupplier_fyassetsupplier_del')" )
    public R removeById(@PathVariable Long id) {
        return R.ok(fyAssetSupplierService.removeById(id));
    }


    /**
     * 导出excel 表格
     * @param fyAssetSupplier 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/exportM")
    @PreAuthorize("@pms.hasPermission('msupplier_fyassetsupplier_export')" )
    public List<AssetSupplierMExcelVO> exportM( AssetSupplierDTO fyAssetSupplier) {
        return fyAssetSupplierService.listExcelTypeM(SpliceQueryWrapper.spliceSearchCondition(fyAssetSupplier));
    }

    /**
     * 导出excel 表格
     * @param fyAssetSupplier 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/exportV")
    @PreAuthorize("@pms.hasPermission('vsupplier_fyassetsupplier_export')" )
    public List<FyAssetSupplierExcelVO> exportV( AssetSupplierDTO fyAssetSupplier) {
        return fyAssetSupplierService.listExcel(SpliceQueryWrapper.spliceSearchCondition(fyAssetSupplier));
    }

	@GetMapping(value = "/tree")
	public R getTree(AssetSupplier supplier) {
		return R.ok(fyAssetSupplierService.selectTree(Wrappers.query(supplier)));
	}

	@GetMapping(value = "/tree/{type}")
	public R getTree(@PathVariable("type") String type) {
        AssetSupplier fyAssetSupplier = AssetSupplier.builder().build();
    	fyAssetSupplier.setType(type);
		return R.ok(fyAssetSupplierService.selectTree(Wrappers.query(fyAssetSupplier)));
	}

    /**
     * 创建供应商/厂商
     * @param type 类型
     * @return R
     */
    @PostMapping("/create/{type}")
    @PreAuthorize("@pms.hasPermission('purchaseApply_fyassetpurchaseapply_add')")
    public R create(@PathVariable("type") String type) {
        AssetSupplier fyAssetSupplier = AssetSupplier.builder().build();
        fyAssetSupplier.setType(type);
        fyAssetSupplier.setUnid(UUID.randomUUID().toString());
        return R.ok(fyAssetSupplier);
    }

    @ApiOperation(value = "批量删除厂商/供应商信息", notes = "批量删除厂商/供应商信息")
    @SysLog("批量删除厂商/供应商信息" )
    @DeleteMapping("/batchDelete" )
    @PreAuthorize("@pms.hasPermission('purchaseApply_fyassetpurchaseapply_del')" )
    public R batchDelete(@RequestBody List<Long> ids) {
        return R.ok(fyAssetSupplierService.removeByIds(ids),String.format("删除成功%s条数据",ids.size()));
    }
}
